<template>
	<el-drawer :title="titleMap[mode]" v-model="visible" :size="600" append-to-body destroy-on-close @closed="$emit('closed')">
		<el-container>
			<el-main class="nopadding">
				<el-scrollbar>
					<el-row class="drawer-table">
						<el-col :span="24">
							<el-form :model="form" :rules="rules" :disabled="mode==='show'" ref="dialogForm" label-width="80px">
								<el-form-item label="项目" prop="purchaseOrderItemId">
									<el-input v-model="form.purchaseOrderItemId" placeholder="请输入项目" clearable></el-input>
								</el-form-item>
								<el-form-item label="采购单" prop="purchaseOrderId">
									<el-input v-model="form.purchaseOrderId" placeholder="请输入采购单" clearable></el-input>
								</el-form-item>
								<el-form-item label="产品" prop="productGgId">
									<el-input v-model="form.productGgId" placeholder="请输入产品" clearable></el-input>
								</el-form-item>
								<el-form-item label="采购数量 最小单位" prop="quantity">
									<el-input v-model="form.quantity" placeholder="请输入采购数量 最小单位" clearable></el-input>
								</el-form-item>
								<el-form-item label="单价" prop="price">
									<el-input v-model="form.price" placeholder="请输入单价" clearable></el-input>
								</el-form-item>
								<el-form-item label="总金额" prop="total">
									<el-input v-model="form.total" placeholder="请输入总金额" clearable></el-input>
								</el-form-item>
								<el-form-item label="折后金额" prop="discount">
									<el-input v-model="form.discount" placeholder="请输入折后金额" clearable></el-input>
								</el-form-item>
                            </el-form>
						</el-col>
					</el-row>
				</el-scrollbar>
			</el-main>
			<el-footer>
				<el-button v-if="mode!=='show'" :loading="isSaveing" @click="submit()" type="primary" size="small">保 存</el-button>
				<el-button size="small" @click="visible=false">取 消</el-button>
			</el-footer>
		</el-container>
	</el-drawer>
</template>
<script>
	import purchaseAPI from '@/api/purchase/purchaseOrderItem';
	export default {
		emits: ['success', 'closed'],
		data() {
			return {
				mode: "add",
				titleMap: {
					add: '新增',
					edit: '编辑',
					mode: ''
				},
				visible: false,
				isSaveing: false,
				//表单数据
				form: {
					purchaseOrderItemId: "",
					purchaseOrderId: "",
					productGgId: "",
					quantity: "",
					price: "",
					total: "",
					discount: "",
				},
				//验证规则
				rules: {
					userName: [
						{required: true, message: '请输入名称'}
					],
					userAge: [
						{pattern: /^[0-9]*$/, message: '年龄需为数字', trigger: 'blur'}
					],
					price: [
						{pattern: /^\d+(\.\d+)?$/, message: '价格需为数字', trigger: 'blur'}
					]
				}
			}
		},
		mounted() {

		},
		methods: {
			//显示
			open(mode='add'){
				this.mode = mode;
				this.visible = true;
				return this;
			},
			//表单提交方法
			submit(){
				this.$refs.dialogForm.validate(async (valid) => {
					if(valid){
						this.isSaveing = true;
						var res = await purchaseAPI.purchaseOrderItem.save.post(this.form);
						this.isSaveing = false;
						if(res.code === 2000){
							this.$emit('success', this.form, this.mode);
							this.visible = false;
							this.$message.success(res.message);
						}else{
							this.$alert(res.message, "提示", {type: 'error'})
						}
					}
				});
			},
			//表单注入数据
			setData(data){
				this.form.purchaseOrderItemId = data.purchaseOrderItemId;
				this.form.purchaseOrderId = data.purchaseOrderId;
				this.form.productGgId = data.productGgId;
				this.form.quantity = data.quantity;
				this.form.price = data.price;
				this.form.total = data.total;
				this.form.discount = data.discount;
			}
		}
	}
</script>
<style>
</style>
